﻿Imports System.Data
Imports ADOEntity
Imports ADOEntityKYC
Partial Class KYC_checkCustomerReview
    Inherits Web.UI.BasePage
#Region "Valiable"
    Dim ds As DataSet
    Dim KYCEntity As KYCEntities = New KYCEntities
    Dim UserEntity As UserEntities = New UserEntities
    Dim clsDataTable As New clsLINQToDataTable
    Dim convertDate As clsDateFormat
    Dim clsCheckLanguage As clsCheckLanguage
    Dim clsDatabase As clsDatabase
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim clsMasterDropDown As clsMasterDropDown
#End Region

#Region "Even Page"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        lblDept.Text = Resources.resUser.DepartmentName
        lblBranch.Text = Resources.resUser.BranchName
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")

            clsSessionMenuRight = Session("clsMenuRight")

            If Not IsPostBack Then
                chkIsLogin()
                ds = New DataSet()
                resourceSet()
                CreateControlMP()
                loadDropDown()
            Else
                ds = Session("ds")
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try

    End Sub
    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub
#End Region

#Region "SetForm"

    Sub resourceSet()

        Dim clsDate As New clsDateFormat
        lblNEXT_REVIEW.Text = clsDateFormat.GetThaiMonth(Now.Month) & " " & clsDateFormat.GetYearLast(clsDateFormat.enmDateCultureInfo.Th)

        'txtStartDate.Text = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        'txtEndDate.Text = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)

    End Sub

    Sub CreateControlMP()
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/Individual.jpg"

        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "รายงาน Daily KYC ETL Sync"
    End Sub

#End Region

#Region " Function"

    Private Sub loadDropDown()
        clsMasterDropDown = New clsMasterDropDown
        Try
            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                                 strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                                 strRoleCode:=clsSessionMenuRight.getRoleGroup)

            clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, _
                                                      strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                strZoneCode:=clsSessionMenuRight.getZoneCode)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    Private Sub IsDataBind()
        Dim clsFormatDate As New clsDateFormat
        Try

            If ds.Tables.Contains("dsCustomerS") Then
                ds.Tables.Clear()
            End If

            '    Where tblCust.CUSTTYPE = 1
            Dim tblKYCETL = From tblKYC In KYCEntity.MCUSTOMER
                              Select tblKYC.CUSTCODE, tblKYC.CUSTOMER_OWNER, tblKYC.PRODUCT_NAME, tblKYC.NEXT_REVIEW, tblKYC.DEPTCODE, tblKYC.BRANCHCODE, tblKYC.DEPUPDATE, tblKYC.UNIQUEID,
                              tblKYC.PASSPORT_NO, tblKYC.THFIRSTNAME, tblKYC.THLASTNAME, tblKYC.THMIDNAME, tblKYC.ENFIRSTNAME, tblKYC.ENLASTNAME, tblKYC.ENMIDNAME,
                              tblKYC.AMLCDD, tblKYC.CUST_COMPLETE_FLAG, tblKYC.LASTKYCUPDATEDATE, tblKYC.LASTKYCUPDATEBY, tblKYC.UPDATEBY, tblKYC.UPDATEDATE, tblKYC.ZONECODE,
                              tblKYC.CONTACTPHONE1, tblKYC.CONTACTPHONE2, tblKYC.CONTACTPHONE3


            If Not (ddlDepartment.SelectedValue = "0") Then
                tblKYCETL = tblKYCETL.Where(Function(cus) cus.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                tblKYCETL = tblKYCETL.Where(Function(cus) cus.BRANCHCODE = ddlBranch.SelectedValue)
            End If



            If Not (ddlSection.SelectedValue = "A") Then
                tblKYCETL = tblKYCETL.Where(Function(cus) cus.DEPUPDATE = ddlSection.SelectedValue)
            End If

            If Not (rdolIsCustComplete.SelectedValue = "A") Then
                tblKYCETL = tblKYCETL.Where(Function(cus) cus.CUST_COMPLETE_FLAG = rdolIsCustComplete.SelectedValue)
            End If
            If (rdoRiskMLFT.SelectedValue = "N") Then
                tblKYCETL = tblKYCETL.Where(Function(cus) cus.AMLCDD Is Nothing)
            ElseIf Not (rdoRiskMLFT.SelectedValue = "A") Then
                tblKYCETL = tblKYCETL.Where(Function(cus) cus.AMLCDD = rdoRiskMLFT.SelectedValue)
            End If


            tblKYCETL = tblKYCETL.Where(Function(cus) Month(cus.NEXT_REVIEW) = Now.Month)
            tblKYCETL = tblKYCETL.Where(Function(cus) Year(cus.NEXT_REVIEW) = Now.Year)



            Dim tblcus = tblKYCETL.ToList



            Dim tblETL = From etljoin In tblKYCETL.ToList Join tblUser In UserEntity.SUSER On etljoin.UPDATEBY.ToString.Trim Equals tblUser.USERNAME.ToString.Trim
                            Order By etljoin.AMLCDD, etljoin.NEXT_REVIEW
                             Select etljoin.CONTACTPHONE1, etljoin.CONTACTPHONE2, etljoin.CONTACTPHONE3, etljoin.CUSTCODE, etljoin.CUSTOMER_OWNER, etljoin.PRODUCT_NAME, etljoin.NEXT_REVIEW, etljoin.DEPTCODE, etljoin.BRANCHCODE, etljoin.DEPUPDATE, etljoin.UNIQUEID,
                              etljoin.PASSPORT_NO, etljoin.THFIRSTNAME, etljoin.THLASTNAME, etljoin.THMIDNAME, etljoin.ENFIRSTNAME, etljoin.ENLASTNAME, etljoin.ENMIDNAME,
                              etljoin.AMLCDD, etljoin.CUST_COMPLETE_FLAG, etljoin.LASTKYCUPDATEDATE, etljoin.LASTKYCUPDATEBY, etljoin.UPDATEBY, etljoin.UPDATEDATE, etljoin.ZONECODE, tblUser.ROLECODE()



            If (clsSessionMenuRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป
            ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then 'Admin ฝ่าย
                tblETL = tblETL.Where(Function(fn) fn.DEPTCODE = clsSessionMenuRight.getDeptCode)
            ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then 'Admin เขต 
                tblETL = tblETL.Where(Function(fn) fn.DEPTCODE = clsSessionMenuRight.getDeptCode And fn.ZONECODE = clsSessionMenuRight.getZoneCode)
            ElseIf (clsSessionMenuRight.getRoleGroup > 49) Then 'Admin สาขา
                tblETL = tblETL.Where(Function(fn) fn.DEPTCODE = clsSessionMenuRight.getDeptCode And fn.BRANCHCODE = clsSessionMenuRight.getBranchCode)
            ElseIf (clsSessionMenuRight.getRoleGroup < 50) Then 'user ทั่วไป
                tblETL = tblETL.Where(Function(fn) fn.DEPTCODE = clsSessionMenuRight.getDeptCode And fn.BRANCHCODE = clsSessionMenuRight.getBranchCode)
            End If


            ds.Tables.Add(clsDataTable.LINQToDataTable(tblKYCETL, "dsCustomerS").Copy)
            Session("dtCustomerReview") = ds.Tables("dsCustomerS")
            grdRiskCustomer.DataSource = ds.Tables("dsCustomerS")
            grdRiskCustomer.DataBind()

            If grdRiskCustomer.Rows.Count = 0 Then
                lblMsg.Visible = True
                lblTotal.Text = ""
            Else
                lblMsg.Visible = False
                lblTotal.Text = grdRiskCustomer.Rows.Count
                lblTotal.Text = "จำนวนรายการทั้งหมด " + ds.Tables("dsCustomerS").Rows.Count.ToString + " รายการ "

            End If




        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub setGrid()

        'grdIndividual.HeaderRow.Cells(0).Visible = False
        'For i As Integer = 0 To grdIndividual.Rows.Count - 1
        '    grdIndividual.Rows(i).Cells(0).Visible = False
        'Next
    End Sub


    Private Sub SaveLogPrint(ByVal index As Integer)
        Try
            Dim program As String = ""
            If Request.QueryString("KYC") = 1 Then
                program = "M11"
            ElseIf Request.QueryString("KYC") = 2 Then
                program = "M21"
            ElseIf Request.QueryString("KYC") = 3 Then
                program = "M31"
            Else
                program = "M41"
            End If

            Dim desc As String = "พิมพ์ฟอร์ม KYC"

            Dim clsLogTransaction As New clsLogTransaction
            '           clsLogTransaction.saveLogTransaction(grdIndividual.Rows(index).Cells(3).Text, clsSessionMenuRight.getUserName, 2, desc, program)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub



    Protected Sub grdRiskCustomer_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdRiskCustomer.SelectedIndexChanged

    End Sub

#End Region
#Region "Even GridView"

    Dim intBeforeItemIndex As Integer

    Protected Sub grdRiskCustomer_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdRiskCustomer.PageIndexChanging

        grdRiskCustomer.PageIndex = e.NewPageIndex
        grdRiskCustomer.SelectedIndex = -1
        grdRiskCustomer.DataSource = ds.Tables("dsCustomerS")
        grdRiskCustomer.DataBind()
        setGrid()
    End Sub


    Protected Sub grdRiskCustomer_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdRiskCustomer.RowDataBound

        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblDate As Label = CType(e.Row.FindControl("lblDate"), Label)
            Dim lblKycInfoReview As Label = CType(e.Row.FindControl("lblKycInfoReview"), Label)
            Dim lblCustomeName As Label = CType(e.Row.FindControl("lblCustomeName"), Label)
            Dim lblRiskLevel As Label = CType(e.Row.FindControl("lblRiskLevel"), Label)
            Dim lblKYCBanchCode As Label = CType(e.Row.FindControl("lblKYCBanchCode"), Label)

            If Not (lblDate.Text = "") Then
                convertDate = New clsDateFormat
                lblDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblDate.Text)
                convertDate = Nothing
            End If
            If Not (lblKycInfoReview.Text = "") Then
                convertDate = New clsDateFormat
                lblKycInfoReview.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblKycInfoReview.Text)
                convertDate = Nothing
            End If

            Dim BRANCHCODE As String = ""
            Dim LASTKYCUPDATEBY As String = ""
            If (DataBinder.Eval(e.Row.DataItem, "BRANCHCODE") IsNot Nothing) Then
                BRANCHCODE = DataBinder.Eval(e.Row.DataItem, "BRANCHCODE").ToString()
            End If
            If (DataBinder.Eval(e.Row.DataItem, "LASTKYCUPDATEBY") IsNot Nothing) Then
                LASTKYCUPDATEBY = DataBinder.Eval(e.Row.DataItem, "LASTKYCUPDATEBY").ToString()
            End If
            If (BRANCHCODE = "" And LASTKYCUPDATEBY = "") Then
                lblKYCBanchCode.Text = ""
            Else
                lblKYCBanchCode.Text = BRANCHCODE & " / " & LASTKYCUPDATEBY
            End If

            Dim AMLCDD As String = ""
            Dim CUST_COMPLETE_FLAG As String = ""
            If (DataBinder.Eval(e.Row.DataItem, "AMLCDD") IsNot Nothing) Then
                AMLCDD = DataBinder.Eval(e.Row.DataItem, "AMLCDD").ToString()
            End If
            If (DataBinder.Eval(e.Row.DataItem, "CUST_COMPLETE_FLAG") IsNot Nothing) Then
                CUST_COMPLETE_FLAG = DataBinder.Eval(e.Row.DataItem, "CUST_COMPLETE_FLAG").ToString()
            End If
            If (AMLCDD = "" And CUST_COMPLETE_FLAG = "") Then
                lblRiskLevel.Text = ""
            Else
                lblRiskLevel.Text = AMLCDD & " / " & CUST_COMPLETE_FLAG
            End If

            Dim THFIRSTNAME As String = ""
            Dim THMIDNAME As String = ""
            Dim THLASTNAME As String = ""
            Dim ENFIRSTNAME As String = ""
            Dim ENMIDNAME As String = ""
            Dim ENLASTNAME As String = ""
            If (DataBinder.Eval(e.Row.DataItem, "THFIRSTNAME") IsNot Nothing) Then
                THFIRSTNAME = DataBinder.Eval(e.Row.DataItem, "THFIRSTNAME").ToString()
            End If

            If (DataBinder.Eval(e.Row.DataItem, "THMIDNAME") IsNot Nothing) Then
                THMIDNAME = DataBinder.Eval(e.Row.DataItem, "THMIDNAME").ToString()
            End If
            If (DataBinder.Eval(e.Row.DataItem, "THLASTNAME") IsNot Nothing) Then
                THLASTNAME = DataBinder.Eval(e.Row.DataItem, "THLASTNAME").ToString()
            End If
            If (DataBinder.Eval(e.Row.DataItem, "ENFIRSTNAME") IsNot Nothing) Then
                ENFIRSTNAME = DataBinder.Eval(e.Row.DataItem, "ENFIRSTNAME").ToString()
            End If
            If (DataBinder.Eval(e.Row.DataItem, "ENMIDNAME") IsNot Nothing) Then
                ENMIDNAME = DataBinder.Eval(e.Row.DataItem, "ENMIDNAME").ToString()
            End If
            If (DataBinder.Eval(e.Row.DataItem, "ENLASTNAME") IsNot Nothing) Then
                ENLASTNAME = DataBinder.Eval(e.Row.DataItem, "ENLASTNAME").ToString()
            End If
            'Dim ENFIRSTNAME As String = DataBinder.Eval(e.Row.DataItem, "ENFIRSTNAME")


            If (THFIRSTNAME = "" And THMIDNAME = "" And THLASTNAME = "" And ENFIRSTNAME = "" And ENMIDNAME = "" And ENLASTNAME = "") Then
                lblCustomeName.Text = ""
            Else
                lblCustomeName.Text = THFIRSTNAME & " " & THMIDNAME & " " & THLASTNAME & " /  <br/>" & ENFIRSTNAME & " " & ENMIDNAME & " " & ENLASTNAME

            End If
        End If

    End Sub

    Protected Sub grdRiskCustomer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdRiskCustomer.SelectedIndexChanged

        If (grdRiskCustomer.SelectedIndex = intBeforeItemIndex) Then
            grdRiskCustomer.SelectedIndex = -1
        End If

    End Sub
    Protected Sub grdRiskCustomer_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdRiskCustomer.SelectedIndexChanging

        intBeforeItemIndex = grdRiskCustomer.SelectedIndex

    End Sub

#End Region

#Region " Even Button "
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try

            IsDataBind()

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Try

            Dim param As String = "{ 'NEXT_REVIEW' : '" & lblNEXT_REVIEW.Text & "'}"
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmChkCustomerReview.aspx','width=1000,height=600,left=100,top=100,resizable=yes,scrollbars=yes','Report'," + param + ");", True)

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub
#End Region

End Class
